package com.iye.puretennis.activity.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import com.iye.puretennis.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: Chuanbao on 2014/11/6.
 */
public class GroupsActivity  extends Activity{
        private static final String GROUP_TEXT = "group_text";//大组成员Map的key
        private static final String CHILD_TEXT1 = "child_text1";//小组成员Map的第一个key
        private static final String CHILD_TEXT2 = "child_text2";//小组成员Map的第二个key

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();//大组成员
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();//小组成员

        ExAdapter adapter;
        ExpandableListView exList;//可扩展的ListView

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.groups);
            //为大小组中添加数据
            for (int i = 1; i < 6; i++) {
                Map<String, String> curGroupMap = new HashMap<String, String>();
                groupData.add(curGroupMap);
                curGroupMap.put(GROUP_TEXT, "第" + i + "大组");

                List<Map<String, String>> children = new ArrayList<Map<String, String>>();
                for (int j = 1; j < 6-i; j++) {
                    Map<String, String> curChildMap = new HashMap<String, String>();
                    children.add(curChildMap);
                    curChildMap.put(CHILD_TEXT1, "第" + j + "小组");
//                    curChildMap.put(CHILD_TEXT2, "第" + j + "小组签名");
                }
                childData.add(children);
            }

            adapter = new ExAdapter(GroupsActivity.this);
            exList = (ExpandableListView) findViewById(R.id.groups);
            exList.setAdapter(adapter);
            exList.setGroupIndicator(null);//不设置大组指示器图标，因为我们自定义设置了
            exList.setDivider(null);//设置图片可拉伸的
        }

        public List<Group> prepareGoups(){
            List<Group> groups = new ArrayList<Group>();

            return groups;
        }
        //关键代码是这个可扩展的listView适配器
        class ExAdapter extends BaseExpandableListAdapter {
            Context context;

            public ExAdapter(Context context) {
                super();
                this.context = context;
            }
            //得到大组成员的view
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                View view = convertView;
                if (view == null) {
                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(R.layout.grouplist, null);
                }

                TextView title = (TextView) view.findViewById(R.id.list_group_name);
                title.setText(getGroup(groupPosition).toString());//设置大组成员名称

                ImageView image = (ImageView) view.findViewById(R.id.group_collapse_icon);//是否展开大组的箭头图标
                if (isExpanded)//大组展开时
                    image.setBackgroundResource(R.drawable.test1);
                else//大组合并时
                    image.setBackgroundResource(R.drawable.test2);

                return view;
            }
            //得到大组成员的id
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }
            //得到大组成员名称
            public Object getGroup(int groupPosition) {
                return groupData.get(groupPosition).get(GROUP_TEXT);
            }
            //得到大组成员总数
            public int getGroupCount() {
                return groupData.size();

            }

            // 得到小组成员的view
            public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
                View view = convertView;
                if (view == null) {
                    LayoutInflater inflater = LayoutInflater.from(context);
                    view = inflater.inflate(R.layout.memberlist, null);
                }
                final TextView title = (TextView) view
                        .findViewById(R.id.child_text);
                title.setText(childData.get(groupPosition).get(childPosition)
                        .get(CHILD_TEXT1));//大标题
//                final TextView title2 = (TextView) view
//                        .findViewById(R.id.child_text2);
//                title2.setText(childData.get(groupPosition).get(childPosition)
//                        .get(CHILD_TEXT2).toString());//小标题

                return view;
            }
            //得到小组成员id
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }
            //得到小组成员的名称
            public Object getChild(int groupPosition, int childPosition) {
                return childData.get(groupPosition).get(childPosition).get(CHILD_TEXT1);
            }
            //得到小组成员的数量
            public int getChildrenCount(int groupPosition) {
                return childData.get(groupPosition).size();
            }
            /**
             * Indicates whether the child and group IDs are stable across changes to the
             * underlying data.
             * 表明大組和小组id是否稳定的更改底层数据。
             * @return whether or not the same ID always refers to the same object
             */
            public boolean hasStableIds() {
                return true;
            }
            //得到小组成员是否被选择
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }

        }


    class Group{
        String guid;
        String name;

        List<Member> members = new ArrayList<Member>();

        Group(String guid, String name,List<Member> members) {
            this.guid = guid;
            this.name = name;
            this.members = members;
        }

        public String getGuid() {
            return guid;
        }

        public void setGuid(String guid) {
            this.guid = guid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<Member> getMembers() {
            return members;
        }

        public void setMembers(List<Member> members) {
            this.members = members;
        }
    }

    class Member{
        private String guid;
        private String name;

        Member(String guid, String name) {
            this.guid = guid;
            this.name = name;
        }

        public String getGuid() {
            return guid;
        }

        public void setGuid(String guid) {
            this.guid = guid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


}
